package faf.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.view.RedirectView;

import faf.business.AdministrationManager;
import faf.entities.Account;

public class GrantRedactorController implements Controller {
	AdministrationManager manager;

	public void setManager(AdministrationManager manager) {
		this.manager = manager;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// check rights
		Account account = (Account) request.getSession().getAttribute("user");
		
		if(account==null) {
			return new ModelAndView(new RedirectView("login.htm?nexturl=administration.htm"));
		}
		if(account.isAdministrator() == false) {
			return new ModelAndView("503");
		}
		long account_id = Long.parseLong(request.getParameter("account_id"));
		
		account = manager.grantRedactorPrivilege(account_id);
		Map map = new HashMap();
		map.put("account", account);
		return new ModelAndView("grantredactor", map);
	}
}
